package dao

import (
	"gorm.io/gorm"
	"gotham-plan-manage/internal/infra/database/mysql/plan/po"
)

/*
   @author:zhongyang
   @date:2023/4/3
   @description:计划详情表数据库操作
*/

type PlanDetailDao struct {
}

func NewPlanDetailDao() *PlanDetailDao {
	return &PlanDetailDao{}
}

func (p *PlanDetailDao) Create(db *gorm.DB, entity *po.PlanDetailPO) error {
	return db.Create(&entity).Error
}

func (p *PlanDetailDao) Delete(db *gorm.DB, detailPO *po.PlanDetailPO) error {
	return db.Delete(detailPO).Error
}

func (p *PlanDetailDao) GetOne(db *gorm.DB, planId string) (*po.PlanDetailPO, error) {
	var res *po.PlanDetailPO
	if err := db.Where("plan_id = ?", planId).First(&res).Error; err != nil {
		return nil, err
	}
	return res, nil
}

func (p *PlanDetailDao) Save(db *gorm.DB, detailPO *po.PlanDetailPO) error {
	return db.Model(&detailPO).Where("plan_id = ?", detailPO.PlanId).Update("target", detailPO.Target).Error
}
